﻿using Basic.FlowApplication.Interface;
using Basic.FlowApplication.Model;
using Basic.FlowApplication.Model.AuditLog;
using Basic.FlowCollect;
using Basic.FlowExtendService;
using Basic.FlowModel;
using Basic.FlowModel.AuditCopyLog;
using Basic.FlowModel.AuditLogItem;
using Basic.FlowRemoteModel;
using Basic.FormRemoteModel;
using Basic.FormRemoteModel.QueryInter.Model;
using Basic.HrRemoteModel.Emp.Model;
using WeDonekRpc.Helper;

namespace Basic.FlowApplication.lmpl
{
    internal class FlowAuditCopyService : IFlowAuditCopyService
    {
        private readonly IEmpService _Emp;
        private readonly IFormInterService _FormInter;
        private readonly IFlowAuditItemLogCollect _AuditItem;
        private readonly IFlowAuditCopyLogCollect _CopyLog;

        public FlowAuditCopyService ( IEmpService emp,
            IFormInterService formInter,
            IFlowAuditCopyLogCollect copy,
            IFlowAuditItemLogCollect auditItem )
        {
            this._CopyLog = copy;
            this._Emp = emp;
            this._FormInter = formInter;
            this._AuditItem = auditItem;
        }

        private AuditFlowBody _FlowBody;
        private AuditLog _Log;
        private readonly List<AuditCopyLogAdd> _Msgs = new List<AuditCopyLogAdd>();
        private readonly List<AuditCopyDto> _SendList = new List<AuditCopyDto>();

        private void _LoadEmp ( AuditItemInfo item, NodeMsg msg )
        {
            msg.EmpId.ForEach(b =>
            {
                this._Msgs.Add(new AuditCopyLogAdd
                {
                    FromEmpId = item.EmpId,
                    ItemId = item.Id,
                    MsgType = msg.MsgType,
                    NodeId = item.NodeId,
                    ToEmpId = b
                });
            });
        }
        private void _LoadForm ( AuditItemInfo item, NodeMsg msg, FlowNode node )
        {
            FormVal val = this._FormInter.GetFormValue(new FormValGetArg
            {
                ColumnId = msg.ColId.Value,
                Keywords = msg.Keywords,
                LinkPK = this._Log.LinkPk,
                LinkType = this._Log.LinkType,
                FormId = node.FormId.GetValueOrDefault(this._FlowBody.DefFormId.Value)
            });
            if ( val.IsNull )
            {
                return;
            }
            else if ( val.DataType == FormRecordDataType.Long )
            {
                this._Msgs.Add(new AuditCopyLogAdd
                {
                    FromEmpId = item.EmpId,
                    ItemId = item.Id,
                    MsgType = msg.MsgType,
                    NodeId = item.NodeId,
                    ToEmpId = val.LongVal.Value
                });
            }
            else if ( val.DataType == FormRecordDataType.Array )
            {
                val.Vals.ForEach(b =>
                {
                    this._Msgs.Add(new AuditCopyLogAdd
                    {
                        FromEmpId = item.EmpId,
                        ItemId = item.Id,
                        MsgType = msg.MsgType,
                        NodeId = item.NodeId,
                        ToEmpId = long.Parse(b)
                    });
                });
            }
        }

        private void _Load ( AuditItemInfo item, NodeMsg msg )
        {
            EmpSearchParam param = new EmpSearchParam
            {
                CompanyId = msg.CompanyId,
            };
            if ( ( FlowMsgReceiveRange.指定部门 & msg.ReceiveRange ) == FlowMsgReceiveRange.指定部门 )
            {
                param.DeptId = msg.DeptId;
            }
            if ( ( FlowMsgReceiveRange.指定单位 & msg.ReceiveRange ) == FlowMsgReceiveRange.指定单位 )
            {
                param.UnitId = msg.UnitId;
            }
            if ( ( FlowMsgReceiveRange.职务 & msg.ReceiveRange ) == FlowMsgReceiveRange.职务 )
            {
                param.Title = msg.TitleCode;
            }
            if ( ( FlowMsgReceiveRange.岗位 & msg.ReceiveRange ) == FlowMsgReceiveRange.岗位 )
            {
                param.Post = msg.PostCode;
            }
            if ( ( FlowMsgReceiveRange.负责人 & msg.ReceiveRange ) == FlowMsgReceiveRange.负责人 )
            {
                param.IsLeader = true;
            }
            int index = 1;
            int skip = 0;
            int size = 50;
            SearchEmpResult res = null;
            do
            {
                res = this._Emp.SearchAuditEmp(param, size, index);
                if ( res.Count == 0 )
                {
                    return;
                }
                skip += size;
                index += 1;
                res.EmpId.ForEach(e =>
                {
                    this._Msgs.Add(new AuditCopyLogAdd
                    {
                        FromEmpId = item.EmpId,
                        ItemId = item.Id,
                        MsgType = msg.MsgType,
                        NodeId = item.NodeId,
                        ToEmpId = e
                    });
                });
            } while ( res.Count > skip );
        }
        private void _CopyBeginMsg ()
        {
            this._Msgs.Clear();
            this._Log.Items.ForEach(c =>
            {
                if ( c.Status == FlowAuditItemStatus.审核进行中 )
                {
                    FlowNode node = this._FlowBody.Nodes[c.NodeId];
                    node.Msgs.ForEach(a =>
                    {
                        if ( a.MsgType != FlowMsgType.审核开始 )
                        {
                            return;
                        }
                        if ( a.ReceiveRange == FlowMsgReceiveRange.指定成员 )
                        {
                            this._LoadEmp(c, a);
                        }
                        else if ( a.ReceiveRange == FlowMsgReceiveRange.流程发起人 )
                        {
                            this._Msgs.Add(new AuditCopyLogAdd
                            {
                                FromEmpId = c.EmpId,
                                ItemId = c.Id,
                                MsgType = a.MsgType,
                                NodeId = c.NodeId,
                                ToEmpId = this._Log.CreateBy
                            });
                        }
                        else if ( a.ReceiveRange == FlowMsgReceiveRange.表单中字段 )
                        {
                            this._LoadForm(c, a, node);
                        }
                        else if ( a.ReceiveRange == FlowMsgReceiveRange.指定节点审批人 )
                        {
                            long[] empId = this._AuditItem.GetNodeAuditEmpId(this._Log.Id, c.RootNodeId, a.ToNodeId);
                            empId.ForEach(e =>
                            {
                                this._Msgs.Add(new AuditCopyLogAdd
                                {
                                    FromEmpId = c.EmpId,
                                    ItemId = c.Id,
                                    MsgType = a.MsgType,
                                    NodeId = c.NodeId,
                                    ToEmpId = e
                                });
                            });
                        }
                        else
                        {
                            this._Load(c, a);
                        }
                    });
                }
            });
            if ( this._Msgs.Count > 0 )
            {
                AuditCopyDto[] msgs = this._CopyLog.Adds(this._Log.Id, this._Msgs.Distinct().ToArray());
                if ( msgs != null )
                {
                    this._SendList.AddRange(msgs);
                }
            }
        }
        public List<AuditCopyDto> CreateCopyMsg ( AuditLog log, AfootAuditLogItem item, AuditFlowBody flow )
        {
            this._Log = log;
            this._FlowBody = flow;
            if ( ConstDic.AuditEndStatus.Contains(item.Status) == false )
            {
                this._CopyBeginMsg();
            }
            if ( item.Status == FlowAuditItemStatus.审核未通过 || item.Status == FlowAuditItemStatus.审核通过 || item.Status == FlowAuditItemStatus.终审通过 )
            {
                this._CopyEndMsg(item.Id);
            }
            return this._SendList;
        }
        private void _CopyEndMsg ( long itemId )
        {
            this._Msgs.Clear();
            AuditItemInfo item = this._Log.Items.Find(c => c.Id == itemId);
            if ( item == null )
            {
                return;
            }
            FlowMsgType msgType = item.Status == FlowAuditItemStatus.审核未通过 ? FlowMsgType.审核未通过 : FlowMsgType.审核通过;
            FlowNode node = this._FlowBody.Nodes[item.NodeId];
            node.Msgs.ForEach(a =>
            {
                if ( a.MsgType != msgType )
                {
                    return;
                }
                if ( a.ReceiveRange == FlowMsgReceiveRange.指定成员 )
                {
                    this._LoadEmp(item, a);
                }
                else if ( a.ReceiveRange == FlowMsgReceiveRange.流程发起人 )
                {
                    this._Msgs.Add(new AuditCopyLogAdd
                    {
                        FromEmpId = item.EmpId,
                        ItemId = item.Id,
                        MsgType = a.MsgType,
                        NodeId = item.NodeId,
                        ToEmpId = this._Log.CreateBy
                    });
                }
                else if ( a.ReceiveRange == FlowMsgReceiveRange.表单中字段 )
                {
                    this._LoadForm(item, a, node);
                }
                else if ( a.ReceiveRange == FlowMsgReceiveRange.指定节点审批人 )
                {
                    long[] empId = this._AuditItem.GetNodeAuditEmpId(this._Log.Id, item.RootNodeId, a.ToNodeId);
                    empId.ForEach(e =>
                    {
                        this._Msgs.Add(new AuditCopyLogAdd
                        {
                            FromEmpId = item.EmpId,
                            ItemId = item.Id,
                            MsgType = a.MsgType,
                            NodeId = item.NodeId,
                            ToEmpId = e
                        });
                    });
                }
                else
                {
                    this._Load(item, a);
                }
            });
            if ( this._Msgs.Count > 0 )
            {
                AuditCopyDto[] msgs = this._CopyLog.Adds(this._Log.Id, this._Msgs.Distinct().ToArray());
                //审核未通过目前只有这会插入数据所以不用再查询一到
                if ( msgs != null && msgType == FlowMsgType.审核未通过 )
                {
                    this._SendList.AddRange(msgs);
                    return;
                }
            }
            if ( msgType == FlowMsgType.审核通过 )
            {
                AuditCopyDto[] msgs = this._CopyLog.GetsCopyMsgs(itemId, FlowMsgType.审核通过);
                if ( !msgs.IsNull() )
                {
                    this._SendList.AddRange(msgs);
                }
            }
        }

        public long[] GetToVoidCopyId ( long[] itemId )
        {
            return this._CopyLog.GetToVoidCopyId(itemId);
        }

        public void SetStatus ( AuditCopyStatus[] sets )
        {
            this._CopyLog.SetStatus(sets);
        }
    }
}
